1661B - Getting Zero - CodeForces Solution


brute force graphs greedy shortest paths

Please click on ads to support us..

Python Code:

cases = int(input())
a = input()
b = list(map(int, a.split()))
for i in range (cases):
  if b[i] == 0:
    print(0, end=' ')
    continue;
  ans = 16
  for j in range(16):
    k = b[i] + j
    for p in range(16):
      if k%2 == 1:
        ans = min(ans, 15 - p + j)
        break
      k //= 2
  print(ans)

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pos "YES"
#define neg "NO"

using namespace std;

int main(){

    fast_io

    ll n;
    cin >> n;

    ll *a = new ll[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];

    ll msk = 0;
    for (int i = 0; i <= 14; i++)
        msk |= (1<<i);

    for (int i = 0; i < n; i++){
        ll ans = 15;

        for (ll ad = 0; ad <= 15; ad++){
            for (ll m = 0; m <= 15; m++){
                if ((((a[i]+ad)<<m)&msk) == 0)
                    ans = min(ans, ad+m);
            }
        }

        cout << ans << " ";
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field